package com.pikaqiu.controller.portal;

import com.pikaqiu.common.Const;
import com.pikaqiu.common.ResponseCode;
import com.pikaqiu.common.ServerResponse;
import com.pikaqiu.pojo.User;
import com.pikaqiu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpSession;
import java.util.zip.ZipInputStream;

/**
 * Created by Administrator on 2018/6/4.
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 用户登录
     *
     * @param username
     * @param password
     * @param SessionU
     * @return
     */
    @RequestMapping(value = "login.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(String username, String password, HttpSession session) {

        ServerResponse<User> login = userService.login(username, password);

        User user = login.getData();

        session.setAttribute(Const.CURRENT_USER, user);

        return login;
    }

    /**
     * 登出接口
     *
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "logout.do", method = RequestMethod.POST)
    public ServerResponse<String> logout(HttpSession session) {

        session.removeAttribute(Const.CURRENT_USER);

        return ServerResponse.createBySuccess();
    }

    /**
     * 注册接口
     *
     * @param user
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "register.do", method = RequestMethod.POST)
    public ServerResponse<String> register(User user) {

        ServerResponse<String> result = userService.register(user);

        return result;
    }

    /**
     * 判断用户名是否存在
     *
     * @param email
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "checkEmail.do", method = RequestMethod.POST)
    public ServerResponse<String> checkEmail(String email) {
        return userService.checkEmail(email);
    }

    /**
     * 判断用户名是否存在
     *
     * @param userName
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "checkUserByName.do", method = RequestMethod.POST)
    public ServerResponse<String> checkUserName(String userName) {
        return userService.checkUserName(userName);
    }

    /**
     * checkUserName
     *
     * @param str
     * @param type
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "checkUserName.do", method = RequestMethod.POST)
    public ServerResponse<String> checkValid(String str, String type) {
        return userService.checkVaild(str, type);
    }

    /**
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "getUserInfo.do", method = RequestMethod.POST)
    public ServerResponse<User> getUserInfo(HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user != null) {
            return ServerResponse.createBySuccess(user);
        }
        return ServerResponse.createByErrorMessage("用户未登录");
    }

    @RequestMapping(value = "forgetGetQuestion.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse forgetGetQuestion(String userName) {
        return userService.selectQuestion(userName);
    }


    @RequestMapping(value = "/forgetCheckAnswer.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse forgetCheckAnswer(String userName, String question, String answer) {
        return userService.checkAnswer(userName, question, answer);
    }

    @ResponseBody
    @RequestMapping(value = "/forgetRestPassword.do", method = RequestMethod.POST)
    public ServerResponse forgetRestPassword(String userName, String newPassword, String token) {
        return userService.forgetRestPassword(userName, newPassword, token);
    }

    @RequestMapping(value = "/restPassword.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse restPassword(HttpSession session, String passwordOld, String passwordNew) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);

        if (user == null) {

            return ServerResponse.createByErrorMessage("用户未登录");
        }
        return userService.restPassword(passwordOld, passwordNew, user);
    }

    @ResponseBody
    @RequestMapping(value = "/updateInformation.do", method = RequestMethod.POST)
    public ServerResponse updateInformation(HttpSession session, User user) {

        User sessionAttribute = (User) session.getAttribute(Const.CURRENT_USER);

        if (sessionAttribute == null) {

            return ServerResponse.createByErrorMessage("用户未登录");
        }
        //防止越权  传入其他的用户的数据
        user.setId(sessionAttribute.getId());

        user.setUsername(sessionAttribute.getUsername());

        ServerResponse<User> userInfo = userService.updateUserInfo(user);

        if (userInfo.isSuccess()) {
            session.setAttribute(Const.CURRENT_USER, userInfo.getData());
        }

        return userInfo;

    }

    @RequestMapping(value = "/getInformation", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getInformation(HttpSession session) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "未登录");
        }
        return userService.getInformation(user.getId());

    }


}
